package com.ggh.jinjilive.ui.mall.collection;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityCollectionBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.collection.CollectionModule;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mall.collection.adapter.CollectionAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;

import java.util.List;


@Route(path = ARouterUtils.CollectionActivity)
public class CollectionActivity extends BaseActivity<ActivityCollectionBinding> implements View.OnClickListener {
    //VM
    private CollectionViewModel collectionViewModel;
    //收藏适配器
    private CollectionAdapter collectionAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        collectionViewModel = new ViewModelProvider(this,viewModelFactory).get(CollectionViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_collection;
    }



    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("收藏",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);

        loadService = LoadSir.getDefault().register(binding.refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取收藏信息
                collectionViewModel.collectProductList();
            }
        });
        loadService.showCallback(LoadingCallback.class);
        //收藏适配器
        collectionAdapter = new CollectionAdapter();
        collectionAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                if(position == 99999){
                    List<CollectionModule> collectionModuleList = (List<CollectionModule>) mObject;
                    boolean allSelected = true;
                    for (CollectionModule collectionModule : collectionModuleList){
                        if(!collectionModule.isSelected()){
                            allSelected = false;
                            break;
                        }
                    }
                    binding.cbCheckall.setChecked(allSelected);
                }
            }
        });
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        binding.recyclerView.setAdapter(collectionAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        //获取收藏信息
        collectionViewModel.collectProductList();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //管理
        binding.managementButton.setOnClickListener(this);
        //全选
        binding.cbCheckall.setOnClickListener(this);
        //删除
        binding.ButtonImmediatelyBuy.setOnClickListener(this);
        //获取收藏信息
        collectionViewModel.mCollectionModuleList.observe(this, new Observer<List<CollectionModule>>() {
            @Override
            public void onChanged(List<CollectionModule> collectionModules) {
                if(null != collectionModules && collectionModules.size() > 0){
                    loadService.showSuccess();
                    collectionAdapter.setAdd(collectionModules);
                }else {
                    loadService.showCallback(EmptyCallback.class);
                    collectionAdapter.setManagement("完成");
                    binding.managementButton.setText("管理");
                    binding.managementButton.setEnabled(false);
                    binding.llDelete.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.management_Button://管理
                String management = binding.managementButton.getText().toString();
                if(!TextUtils.isEmpty(management)){
                    if(management.equals("管理")){
                        collectionAdapter.setManagement("管理");
                        binding.managementButton.setText("完成");
                        binding.llDelete.setVisibility(View.VISIBLE);
                    }else if(management.equals("完成")){
                        collectionAdapter.setManagement("完成");
                        binding.managementButton.setText("管理");
                        binding.llDelete.setVisibility(View.GONE);
                    }
                }
                break;
            case  R.id.cb_checkall://全选
                if(binding.cbCheckall.isChecked()){
                    selectAll(true);
                }else {
                    selectAll(false);
                }
                break;
            case R.id.Button_immediately_buy://删除
                //删除
                delCollectProduct();
                break;
        }
    }

    /**
     * 全选
     */
    private void selectAll(boolean selectAll){
        if(collectionViewModel.mCollectionModuleList.getValue().size() > 0){
            for (int i = 0;i < collectionViewModel.mCollectionModuleList.getValue().size() ;i++){
                collectionViewModel.mCollectionModuleList.getValue().get(i).setSelected(selectAll);
            }
        }
        collectionAdapter.notifyDataSetChanged();
    }

    /**
     * 删除
     */
    private void delCollectProduct(){
        String deleteList = "";
        if(collectionViewModel.mCollectionModuleList.getValue().size() > 0){
            for (int i = 0;i < collectionViewModel.mCollectionModuleList.getValue().size() ;i++){
                if(collectionViewModel.mCollectionModuleList.getValue().get(i).isSelected()){
                    if(TextUtils.isEmpty(deleteList)){
                        deleteList = collectionViewModel.mCollectionModuleList.getValue().get(i).getId() + "";
                    }else {
                        deleteList = deleteList +","+ collectionViewModel.mCollectionModuleList.getValue().get(i).getId();
                    }
                }
            }
        }
        collectionViewModel.delCollectProduct(deleteList);
    }


}
